
import { request } from "./request"
export const handlewxLogin = async() => {
    return new Promise((resolve, reject) => {
            uni.getUserProfile({
            desc: '用于完善会员资料',
            success: async (profileRes) => {
            const userInfo = profileRes.userInfo;
            console.log('用户信息', profileRes)

            // ✅ Step 2: 再去获取 code
            const loginRes = await new Promise((resolve, reject) => {
                uni.login({
                    provider: 'weixin',
                    success: resolve,
                    fail: reject
                });
            });

            const code = loginRes.code;
            const params = {
                code: code,
                platform: 1,
                userInfo: userInfo
            };
            // ✅ Step 3: 提交给后端
            const res = await request({
                url: '/user/login',
                method: 'POST',
                data: params
            })
                uni.setStorageSync('token', res.data.accessToken)
                uni.setStorageSync('refreshToken', res.data.refreshToken)
                await getUserInfo()
                console.log('登录成功：', res);
                resolve(res)
            },
            fail: (err) => {
                console.log('用户拒绝授权：', err);
                reject(err)
            }
        });
    })

}

export const onGetUserInfo = async(e) => {
    return new Promise((resolve, reject) => {
        const { userInfo, errMsg } = e.detail; // 获取用户信息
        uni.getUserProfile({
            desc: '用于完善会员资料',
            success: async (profileRes) => {
                const userInfo = profileRes.userInfo;
                const loginRes = await new Promise((resolve, reject) => {
                    uni.login({
                    provider: 'toutiao',
                        success: res => resolve(res),
                        fail: err => reject(err)
                    })
                });
                const {code} = loginRes;
                if (code) {
                    // 登录成功，获取到 anonymousCode
                    const params = {
                        code: code,
                        platform: 2,
                        userInfo: userInfo
                        };
                        console.log('登录成功：', params);
                    // 将 anonymousCode 发送给后端
                    const res = await request({
                        url: '/user/login',
                        method: 'POST',
                        data: params
                    })
                    uni.setStorageSync('token', res.data.accessToken)
                    uni.setStorageSync('refreshToken', res.data.refreshToken)
                    console.log('登录成功：', res);
                    await getUserInfo()
                    resolve(res)
                } else {
                    console.error('获取 anonymousCode 失败:', errMsg)
                    reject(err)
                }
            }
        })
        
    })
    
}

// 用户信息
const getUserInfo = async() => {
    await request({
        url: '/user/info',
        method: 'POST',
        data: {}
    }).then(res => {
        console.log('info', res.data)
        uni.setStorageSync('userInfo', res.data)
        // uni.switchTab({
        //     url: '/pages/index/index'
        // })
    })
}